package com.hundun.vision.biz.core;

import org.ta4j.core.BarSeries;
import org.ta4j.core.BaseStrategy;
import org.ta4j.core.Rule;
import org.ta4j.core.Strategy;
import org.ta4j.core.indicators.RSIIndicator;
import org.ta4j.core.indicators.SMAIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.indicators.helpers.VolumeIndicator;
import org.ta4j.core.num.DecimalNum;
import org.ta4j.core.rules.OverIndicatorRule;
import org.ta4j.core.rules.StopLossRule;
import org.ta4j.core.rules.UnderIndicatorRule;

import java.math.BigDecimal;

/**
 * @author ：RuoChen
 * @date ：14:10 2024/2/27
 * @description：综合空单策略
 */
public class CombineSellStrategy {
    private static final String name = "综合空单策略";

    /**
     * 构建交易策略
     *
     * @param barSeries
     * @param stopLossPercentage
     * @return
     */
    public static Strategy buildStrategy(BarSeries barSeries, BigDecimal stopLossPercentage) {
        ClosePriceIndicator closePrice = new ClosePriceIndicator(barSeries);
        VolumeIndicator volume = new VolumeIndicator(barSeries);
        SMAIndicator shortSma = new SMAIndicator(closePrice, 50);
        SMAIndicator longSma = new SMAIndicator(closePrice, 200);
        RSIIndicator rsi = new RSIIndicator(closePrice, 14);
        /**
         * 入场规则：短期SMA跌破长期SMA（下降趋势）、RSI大于70、成交量增加
         */
        Rule entryRule = new UnderIndicatorRule(shortSma, longSma)
                .and(new OverIndicatorRule(rsi, 70))
                .and(new OverIndicatorRule(volume, new SMAIndicator(volume, 10)));
        /**
         * 出场规则：短期SMA突破长期SMA（趋势反转）、RSI小于30
         */
        Rule exitRule = new OverIndicatorRule(shortSma, longSma)
                .or(new UnderIndicatorRule(rsi, 30))
                .or(new StopLossRule(closePrice, DecimalNum.valueOf(stopLossPercentage)));
        return new BaseStrategy(name, entryRule, exitRule);
    }
}
